package com.studentSystem.controller;

import com.studentSystem.pojo.Teacher;
import com.studentSystem.service.TeacherService;
import com.studentSystem.utils.*;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/teacher")
@Validated
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/register")
    public ResultUtil register(
            @Pattern(regexp = "^\\S{1,8}$") String name,
            @Pattern(regexp = "^\\S{5,16}$") String password,
            @Pattern(regexp = "^1[3-9]\\d{9}$") String phoneNumber) {
        if (teacherService.findByPhoneNumber(phoneNumber)!=null){
            return ResultUtil.error("该手机号已被注册");
        }
        teacherService.register(name, password, phoneNumber);
        return ResultUtil.success();
    }

    @PostMapping("/login")
    public ResultUtil<String> login(String phoneNumber, @Pattern(regexp = "^\\S{5,16}$") String password) {
        Teacher loginUser = teacherService.findByPhoneNumber(phoneNumber);
        if (loginUser == null) {
            return ResultUtil.error("手机号错误");
        }

        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("name", loginUser.getName());
            String token = JwtUtil.genToken(claims);

            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set(token, token, 1, TimeUnit.HOURS);
            return ResultUtil.success(token);
        }
        return ResultUtil.error("密码错误");
    }

    @GetMapping("/userInfo")
    public ResultUtil<Teacher> userInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Long id = (Long) map.get("id");
        Teacher user = teacherService.findById(id);
        return ResultUtil.success(user);
    }

    @PatchMapping("/updatePwd")
    public ResultUtil updatePwd(@RequestBody Map<String, String> params, @RequestHeader("Authorization") String token) {
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return ResultUtil.error("缺少必要的参数");
        }

        Map<String, Object> map = ThreadLocalUtil.get();
        Long id = (Long) map.get("id");
        Teacher loginUser = teacherService.findById(id);
        if (!loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))) {
            return ResultUtil.error("原密码填写不正确");
        }

        if (!rePwd.equals(newPwd)) {
            return ResultUtil.error("两次填写的新密码不一样");
        }

        teacherService.updatePwd(newPwd);
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.getOperations().delete(token);
        return ResultUtil.success();
    }

    @GetMapping("/list")
    public ResultUtil<List<Teacher>> list() {
        List<Teacher> pb = teacherService.list();
        return ResultUtil.success(pb);
    }

    @PutMapping("/update")
    public ResultUtil update(@RequestBody @Validated(Teacher.Update.class)Teacher teacher) {
        teacherService.update(teacher);
        return ResultUtil.success();
    }
}
